package cn.bonoon.kernel.web;

/**
 * <pre>
 * 定义某一按钮对应的处理事件的类型，包括：
 * 1.弹出对话框
 * 2.GET方式提交
 * 3.POST方式提交
 * 4.FORM提交
 * </pre>
 * @author jackson
 *
 */
public enum ButtonEventType {
	NONE,
	/**
	 * 弹出对话框
	 */
	DIALOG{
		@Override
		public void parse(StringBuilder js, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate) {
			js.append("jQuery('#div-kernel-dialog-loader').load(__toUrl(this.href));return false;");
		}
	},
	/**
	 * 提交form表单
	 */
	FORM{
		@Override
		public void parse(StringBuilder js, String key, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate) {
			js.append("jQuery(this).__form('").append(key).append("','").append(name).append("', function(){");
			if(null != beforeOperate){
				js.append(beforeOperate);
			}
			js.append('}');
			if(refresh == ButtonRefreshType.FINISH){
				js.append(", function(__resultData){").append(afterOperate).append('}');
			}
			js.append(");return false;");
		}
	},
	/**
	 * 以post的方式提交数据;ajax方式
	 */
	POST{
		@Override
		public void parse(StringBuilder js, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate) {
			if(null != beforeOperate){
				js.append(beforeOperate);
			}
			js.append("jQuery(this).__post('").append(name).append("', __data");
			if(refresh == ButtonRefreshType.FINISH){
				js.append(", function(__resultData){").append(afterOperate).append('}');
			}else if(refresh==ButtonRefreshType.REFRESH){
				//js.append("");
				js.append(", function(__resultData){location.reload();}");
			}
			js.append(");return false;");
		}
	},
	/**
	 * 通过get方式来取数据;ajax方式
	 */
	GET{
		@Override
		public void parse(StringBuilder js, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate) {
			if(null != beforeOperate){
				js.append(beforeOperate);
			}
			js.append("jQuery(this).__get('").append(name).append('\'');
			if(refresh == ButtonRefreshType.FINISH){
				js.append(", function(__resultData){").append(afterOperate).append('}');
			}
			js.append(");return false;");
		}
	},
	/**
	 * 在本页面打开，即：&lt;a href='url'&gt;按钮名称&lt;/a&gt;
	 */
	LOAD,
	
//	DOWNLOAD{
//		@Override
//		public void parse(StringBuilder js, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate) {
//			if(null != beforeOperate){
//				js.append(beforeOperate);
//			}
//			//js.append("&lt;a href='url'&gt;").append(name).append("&lt;/a&gt;");
//			js.append("jQuery(this).__post('").append(name).append("', __data");
//			//js.append(", function(__resultData){$.unblockUI();process(__resultData);},'xml'");
//			js.append(", function(__resultData){").append(afterOperate).append('}');
//			js.append(");return false;");
//			//System.out.println(js);
//		}
//	},
	/**
	 * 在新的页面打开，即：&lt;a href='url' target='_blank'&gt;按钮名称&lt;/a&gt;
	 */
	JUMP;
	
	/**
	 * <pre>
	 * 这个方法只处理以下几种情况：
	 * 1.{@link #DIALOG}
	 * 2.{@link #POST}
	 * 3.{@link #GET}
	 * </pre>
	 * 
	 * @param js
	 * @param name
	 * @param refresh
	 * @param refreshParent
	 */
	public void parse(StringBuilder js, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate){ }

	public void parse(StringBuilder js, String key, String beforeOperate, String name, ButtonRefreshType refresh, String afterOperate){
		parse(js, beforeOperate, name, refresh, afterOperate);
	}
}
